package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.feign.OrderPayFeignApi;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private OrderPayFeignApi orderPayFeignApi;

    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;

    @RequestMapping("/alipay")
    public Result<String> alipay(String orderNo, int type){
        //参数校验
        if(StringUtils.isEmpty(orderNo) || StringUtils.isEmpty(type + "")){
            throw new BusinessException(SeckillCodeMsg.PARAM_ERROR);
        }
        //判断支付类型
        if(OrderInfo.PAYTYPE_ONLINE == type){
            //在线支付
            String result = orderInfoService.payOnline(orderNo);
            return Result.success(result);
        }else if (OrderInfo.PAYTYPE_INTERGRAL == type){
            //积分支付
            orderInfoService.payIntergral(orderNo);
        }else{
            throw new BusinessException(SeckillCodeMsg.PARAM_ERROR);
        }
        return Result.success("");
    }

    /**
     * 同步回调
     */
    @RequestMapping("/return_url")
    public void return_url(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        //远程调用支付服务进行验签操作
        Result<Boolean> result = orderPayFeignApi.checkSign(params);
        //获取订单编号
        String orderNo = params.get("out_trade_no");
        if(result != null && !result.hasError()){
            if(result.getData()) {
                //验签成功
                response.sendRedirect(frontEndPayUrl + orderNo);
            }else {
                //验签失败
                response.sendRedirect("http://localhost/error.html");
            }
        }
    }

    /**
     * 异步回调
     */
    @RequestMapping("/notify_url")
    public void notify_url(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        //获取响应流对象
        PrintWriter out = response.getWriter();
        //调用支付服务验签
        Result<Boolean> result = orderPayFeignApi.checkSign(params);
        //获取订单号
        String orderNo = params.get("out_trade_no");
        if(result != null && !result.hasError()){
            if(result.getData()){//验证成功
                //支付成功业务
                orderInfoService.payOnlineSuccess(orderNo);
                out.println("success");
            }else {
                out.println("fail");
            }
        }
    }

    /**
     * 退款业务
     */
    @RequestMapping("/refund")
    public Result<String> refund(String orderNo){
        OrderInfo orderInfo = orderInfoService.find(orderNo);
        if(orderInfo == null){
            throw new BusinessException(SeckillCodeMsg.PARAM_ERROR);
        }
        //判断支付类型,执行不同类型的执行流程
        if(OrderInfo.PAYTYPE_ONLINE == orderInfo.getPayType()){
            //支付宝退款流程
            orderInfoService.alipayRefund(orderInfo);
        }else if(OrderInfo.PAYTYPE_INTERGRAL == orderInfo.getPayType()){
            //积分退款流程
            orderInfoService.intergralRefund(orderInfo);
        }else {
            throw new BusinessException(SeckillCodeMsg.REFUND_ERROR);
        }
        return Result.success("");
    }

    @RequestMapping("/cancelOrder")
    public Result<String> cancelOrder(String orderNo){
        OrderInfo orderInfo = orderInfoService.find(orderNo);
        if(orderInfo == null){
            throw new BusinessException(SeckillCodeMsg.PARAM_ERROR);
        }
        orderInfoService.cancelOrder(orderInfo);
        return Result.success("取消成功");
    }
}
